#region

using System;
using System.ComponentModel;
using System.Windows.Forms;

#endregion

namespace MediaController.Extractor
{
    public class WindowStateMonitor : ISupportInitialize
    {
        
        protected WeakReference form;
        protected bool hooked;
        protected string name;
        protected object tag;
        protected FormWindowState windowState;

        // Events

        
        public WindowStateMonitor()
        {
        }

        public WindowStateMonitor(Form form)
        {
            this.form = new WeakReference(form);
            Hook();
        }

        public Form Form
        {
            get { return (Form) form.Target; }
            set { form = new WeakReference(value); }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public object Tag
        {
            get { return tag; }
            set { tag = value; }
        }

        #region ISupportInitialize Members

        public void BeginInit()
        {
        }

        public void EndInit()
        {
            if ((form != null) && !hooked)
            {
                Hook();
            }
        }

        #endregion

        public event EventHandler Maximized;

        public event EventHandler Minimized;

        public event EventHandler Normalized;

        public event EventHandler Restored;

        public void Hook()
        {
            windowState = ((Form) form.Target).WindowState;
            ((Form) form.Target).Layout += new LayoutEventHandler(OnLayout);
        }

        private void OnLayout(object sender, LayoutEventArgs e)
        {
            if (((Form) form.Target).WindowState != windowState)
            {
                switch (((Form) form.Target).WindowState)
                {
                    case FormWindowState.Normal:
                        if (Normalized != null)
                        {
                            Normalized(this, EventArgs.Empty);
                        }
                        if ((windowState == FormWindowState.Minimized) && (Restored != null))
                        {
                            Restored(this, EventArgs.Empty);
                        }
                        break;

                    case FormWindowState.Minimized:
                        if (Minimized != null)
                        {
                            Minimized(this, EventArgs.Empty);
                        }
                        break;

                    case FormWindowState.Maximized:
                        if (Maximized != null)
                        {
                            Maximized(this, EventArgs.Empty);
                        }
                        if ((windowState == FormWindowState.Minimized) && (Restored != null))
                        {
                            Restored(this, EventArgs.Empty);
                        }
                        break;
                }
                windowState = ((Form) form.Target).WindowState;
            }
        }

        // Properties
    }
}